﻿// SilverlightBridge
// Copyright (c) 2009
// by Open Light Group
// http://openlightgroup.net/
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
//
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace SilverlightFileManager
{
    public partial class FileManager_Default : System.Web.UI.Page
    {

        public string SilverlightApplication { get; set; }
        public string SilverlightInitParams { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            // Must be a logged in            
            if (!User.Identity.IsAuthenticated)
            {
                Response.Redirect("../Default.aspx");
                Response.End();
            }

            string strSilverlightApplication = @"../ClientBin/FileManager.xap";

            // Register Silverlight.js file
            //Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "SilverlightJS",
            //    (this.TemplateSourceDirectory + "/Silverlight.js"));

            // Set the path to the .xap file
            SilverlightApplication = String.Format("{0}{1}", TemplateSourceDirectory,
                String.Format(@"/{0}", strSilverlightApplication));

            int intUserID = GetUserID();
            string strIPAddress = this.Context.Request.UserHostAddress;
            string strPassword = Authendication.SetSilverlightKey(intUserID, strIPAddress);
            string strWebServiceBase = GetWebServiceBase();

            SilverlightInitParams =
                String.Format("PortalID={0},ModuleID={0},UserID={0},Password={1},WebServiceBase={2}",
                intUserID.ToString(), strPassword, strWebServiceBase);
        }

        #region GetWebServiceBase
        private string GetWebServiceBase()
        {
            string strWebServiceBase = String.Format(@"{0}://", this.Request.Url.Scheme);
            strWebServiceBase = String.Format(@"{0}{1}", strWebServiceBase, this.Request.Url.Host.Replace(@"\", @"/"));
            strWebServiceBase = String.Format(@"{0}{1}", strWebServiceBase, ((this.Request.Url.Port == 80) ? "" : ":" + this.Request.Url.Port.ToString()));
            strWebServiceBase = String.Format(@"{0}{1}", strWebServiceBase, this.Request.ApplicationPath.Replace(@"\", ""));
            strWebServiceBase = String.Format(@"{0}/webservice/", strWebServiceBase);
            return strWebServiceBase;
        }
        #endregion

        #region GetUserID
        private int GetUserID()
        {
            int intUserID = -1;
            if (User.Identity.IsAuthenticated)
            {
                SilverlightFileManagerDALDataContext SilverlightFileManagerDALDataContext = new SilverlightFileManagerDALDataContext();

                var SilverlightBridgeUser = from DesktopUser in SilverlightFileManagerDALDataContext.SilverlightBridgeUsers
                                             where DesktopUser.UserName == User.Identity.Name
                                             select DesktopUser;

                intUserID = SilverlightBridgeUser.FirstOrDefault().UserID;
            }
            return intUserID;
        }
        #endregion
    }
}